package homework;

import java.util.Stack;

//作业标题(2738)
//根据二叉树创建字符串
//
//作业内容
//根据二叉树创建字符串
//https://leetcode.cn/problems/construct-string-from-binary-tree/description/
public class h2738 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public String tree2str(TreeNode root) {
        if(root == null) return "";
        if(root.left == null && root.right == null) return Integer.toString(root.val);
        if(root.right == null) return new StringBuffer().append(root.val).append("(").append(tree2str(root.left)).append(")").toString();
        return new StringBuffer().append(root.val).append("(").append(tree2str(root.left)).append(")(").append(tree2str(root.right)).append(")").toString();
    }
}
